package tool

import java.io.File
import java.net.URLEncoder

import org.apache.commons.io.FileUtils
import play.api.libs.Files.TemporaryFile
import play.api.libs.json.Json
import play.api.mvc.{MultipartFormData, Request, RequestHeader}
import shapeless.{HList, LabelledGeneric}
import shapeless.ops.record.ToMap
import tool.Pojo.MyDataDir
import tool.Tool.getUserIdDir
import utils.Utils
import utils.Utils.{getArrayByTs, getValue}

import scala.collection.JavaConverters._

/**
 * Created by Administrator on 2019/12/6
 */
object WebTool {

  def getDataJson(file: File) = {
    val lines = FileUtils.readLines(file).asScala
    val sampleNames = lines.head.split("\t").drop(1)
    val array = lines.drop(1).map { line =>
      val columns = line.split("\t")
      val map = Map(("geneId" -> columns(0)))
      val otherMap = sampleNames.zip(columns.drop(1)).map { case (sampleName, data) =>
        (sampleName -> data)
      }.toMap
      map ++ otherMap
    }
    Json.obj("array" -> array, "sampleNames" -> sampleNames)
  }

  def getUserMissionDir(implicit request: RequestHeader) = {
    val userIdDir = getUserIdDir
    new File(userIdDir, "mission")
  }

  def getUserStepMissionDir(implicit request: RequestHeader) = {
    val userIdDir = getUserIdDir
    new File(userIdDir, "step_mission")
  }

  def getMissionIdDir(missionId: Int)(implicit request: RequestHeader) = {
    val userMissionFile = getUserMissionDir
    new File(userMissionFile, missionId.toString)
  }

  def getStepMissionIdDir(missionId: Int)(implicit request: RequestHeader) = {
    val userMissionFile = getUserStepMissionDir
    new File(userMissionFile, missionId.toString)
  }

  def getMissionWorkspaceDirById(missionId: Int)(implicit request: RequestHeader) = {
    val missionIdDir = getMissionIdDir(missionId)
    new File(missionIdDir, "workspace")
  }


}
